/**
 * 
 */
package com.jie.eclipse.pan.popup.wizardpage.grid;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.ui.forms.events.ExpansionAdapter;
import org.eclipse.ui.forms.events.ExpansionEvent;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.wst.common.ui.internal.dnd.ObjectTransfer;
import org.eclipse.wst.common.ui.internal.dnd.ViewerDragAdapter;

import com.jie.eclipse.pan.bind.BaseDataBind;
import com.jie.eclipse.pan.config.BasePageConfig;
import com.jie.eclipse.pan.dao.QMDao;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.editors.utils.PanImageHelper;
import com.jie.eclipse.pan.form.item.Tab;
import com.jie.eclipse.pan.form.item.Table;
import com.jie.eclipse.pan.form.item.table.Tbody;
import com.jie.eclipse.pan.form.item.table.TbodyTd;
import com.jie.eclipse.pan.form.item.table.TbodyTr;
import com.jie.eclipse.pan.form.item.table.Thead;
import com.jie.eclipse.pan.form.item.table.TheadTd;
import com.jie.eclipse.pan.form.item.table.TheadTr;
import com.jie.eclipse.pan.form.support.FormItem;
import com.jie.eclipse.pan.gef.action.GridSeachActionGroup;
import com.jie.eclipse.pan.gef.action.SelectedColumnsActionGroup;
import com.jie.eclipse.pan.gef.dialog.BaseDialog;
import com.jie.eclipse.pan.gef.listener.PanTableOrderDropAdapter;
import com.jie.eclipse.pan.listener.PMouseListener;
import com.jie.eclipse.pan.listener.PSelectionListener;
import com.jie.eclipse.pan.popup.wizardpage.BasePage;
import com.jie.eclipse.pan.swtitem.table.PTable;
import com.jie.eclipse.pan.swtitem.table.PTableColumn;
import com.jie.eclipse.pan.swtitem.table.PTableViewer;
import com.jie.eclipse.pan.swtitem.table.PllTableContentProvider;
import com.jie.eclipse.pan.swtitem.table.PllTableLabelProvider;
import com.jie.eclipse.pan.swtitem.table.cellmodifier.SelectColumnsTableCellModifier;
import com.jie.eclipse.pan.swtitem.widgets.BRComp;
import com.jie.eclipse.pan.swtitem.widgets.PanStringComboBoxCellEditor;
import com.jie.eclipse.pan.utils.ObjectHelper;
import com.jie.eclipse.pan.utils.StringBuilderEx;
import com.jie.eclipse.pan.utils.StringHelper;
import com.jie.eclipse.pan.utils.Utils;

/**
 * @author yanjie
 *
 */
public class Grid1CrudPage extends BasePage{
	/**
	 * @param pageName
	 */
	public Grid1CrudPage() {
		super("page_left","",PanImageHelper.getInstance().getPanImageDescriptor());
		//设置标题消息
		this.setMessage("表格页面配置");
	}
	
	@Override
	public void createControl(Composite parent) {
		BaseDataBind dataBind = this.getDataBind();
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(1, true));
		
		Color bgcolor = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		
//		SashForm sashForm = new SashForm(composite, SWT.VERTICAL);
//		sashForm.setLayoutData((new GridData(GridData.FILL_BOTH)));
		
		// 创建数据源区域的面板
		createDataSourceComposite(this,dataBind, composite);
		// 创建表单属性的表格信息面板
		createDateGridComposite(this,dataBind, composite);

		setControl(composite);
	}
	
	
	/**
	 * @param gridComposite
	 * @param cs
	 *            创建数据源区域的面板
	 */
	public void createDataSourceComposite(BasePage basePage, BaseDataBind tabbind,Composite tabcp) {
		FormToolkit toolkit = new FormToolkit(tabcp.getDisplay());
		Color bgcolor = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		ExpandableComposite ec = toolkit.createExpandableComposite(tabcp,
				ExpandableComposite.TREE_NODE
						| ExpandableComposite.NO_TITLE_FOCUS_BOX);
		ec.setBackground(bgcolor);
		ec.setData("basePage", basePage);
		ec.setText("根据SQL配置查询条件及表格");
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
//		gd.horizontalSpan=2;
		ec.setLayoutData(gd);
		ec.addExpansionListener(new ExpansionAdapter() {
			public void expansionStateChanged(ExpansionEvent e) {
				// 根据部件的新尺寸重新定位和更新滚动条
				Composite ec = (ExpandableComposite) e.getSource();
				ec.layout();
				ec.getParent().layout();
//				ec.getParent().getParent().getParent().getParent().layout();

				BasePage basePage = (BasePage) ec.getData("basePage");
//				basePage.getShell().pack(true);
//				Utils.print("x="+basePage.getShell().getSize().x+"\r\ny="+basePage.getShell().getSize().y);
				basePage.getShell().setSize(843,800);
			}
		});

		Composite group1 = toolkit.createComposite(ec, SWT.NONE);
		group1.setLayout(new GridLayout(3, false));
		group1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		ec.setClient(group1);

		Label labe11 = new Label(group1, SWT.RIGHT);
		labe11.setText("DataSource:");
		Combo combo1 = new Combo(group1, SWT.READ_ONLY);
		String[] datasources = QMDao.getPanAllDataSource(false);
		combo1.setItems(datasources);
		combo1.setText(this.getQmDao().getDefaultDataSource());
		combo1.setLayoutData(new GridData(100, 24));
		for (int i = 0; i < datasources.length; i++) {
			combo1.setData(String.valueOf(i), datasources[i]);
		}
		this.getDataBind().bind("dataSource", combo1);

		Button button1 = new Button(group1, SWT.NONE);
		button1.setText("语句分析");

		Label labe12 = new Label(group1, SWT.RIGHT);
		labe12.setText("SQl:");		
		Text text2 = new Text(group1, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		GridData text2GridData = new GridData(GridData.FILL_HORIZONTAL);
		text2GridData.horizontalSpan = 2;
		text2GridData.heightHint = 50;
		text2.setLayoutData(text2GridData);
		text2.setText(this.getQmDao().getDefaultSQL());
		this.getDataBind().bind("sql", text2);

		Label labe13 = new Label(group1, SWT.RIGHT);
		labe13.setText("可选栏位:");

		ViewForm vform = new ViewForm(group1, SWT.NONE);
		// vform.setLayout(new FillLayout());
		vform.setLayout(new GridLayout());
		GridData vformData = new GridData(GridData.FILL_HORIZONTAL);
		vformData.horizontalSpan = 2;
		vformData.heightHint = 150;
		vform.setLayoutData(vformData);

		PTableViewer tv1 = createTableViewer1(vform);
		CheckboxTableViewer ctv1 = new CheckboxTableViewer(tv1.getTable());
		BaseDataEntity data1 = this.getDataInstance();
		data1.spov("GridCrudPage"+"tv1", tv1);
		data1.spov("GridCrudPage"+"ctv1", ctv1);

		tv1.setContentProvider(new PllTableContentProvider()); // 内容器
		tv1.setLabelProvider(new PllTableLabelProvider(tv1));// 标签器

		GridSeachActionGroup actionGroup1 = new GridSeachActionGroup(this,"GridCrudPage","GridCrudPage_SQL");
		actionGroup1.fillContextMenu(new MenuManager());
		ToolBar toolbar1 = new ToolBar(vform, SWT.FLAT | SWT.RIGHT);
		toolbar1.setBackground(bgcolor);
		ToolBarManager toolbarManager = new ToolBarManager(toolbar1);
		actionGroup1.fillActionToolbars(toolbarManager);

		vform.setContent(tv1.getControl());
		vform.setTopLeft(toolbar1);

		BaseDataEntity data2 = new BaseDataEntity();
		data2.spov("tv", tv1);
		data2.spov("bind", this.getDataBind());

		button1.addMouseListener(new PMouseListener(data2) {
			public void mouseUp(MouseEvent paramMouseEvent) {
				QMDao qmDao = QMDao.getInstance();
				BaseDataBind bind = (BaseDataBind) this.gpov("bind");
				PTableViewer tv1 = (PTableViewer) this.gpov("tv");
				String sql = bind.getValue("sql");
				tv1.setInput(qmDao.getColumnsInfo(bind.getValue("dataSource"),
						sql));
				String tablename = getQmDao().getTableNameBySql(sql);
				if(tablename.length() > 0){
					bind.setValue("pkAliasName",tablename.replace("..",",").split(",")[1]+bind.getValue("pkname"));
				}
				
				if(!StringHelper.IsNullOrEmpty(tablename)){
					tv1.setInput(qmDao.getColumnsInfo(bind.getValue("datasource"),sql));
					bind.setValue("tablename", tablename);
					
					if(StringHelper.IsNullOrEmpty(bind.getValue("dataname"))){
						bind.setValue("dataname", getQmDao().getDatanameByTablename(tablename));
					}
				}else{
					Utils.print("SQL语句不合法!");
				}
			}
		});
	}

	/**
	 * @param group1
	 * @return 可选栏位的表格查看器
	 */
	public PTableViewer createTableViewer1(ViewForm vform) {
		PTableViewer tv1 = new PTableViewer(new PTable(vform, SWT.CHECK
				| SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER
				| SWT.FULL_SELECTION));
		PTable table = tv1.getPTable();
		table.setLayout(new GridLayout());
		GridData tableGridData = new GridData(GridData.FILL_BOTH);
		// tableGridData.horizontalSpan=2;
		// tableGridData.heightHint = 100;
		table.setLayoutData(tableGridData);
		table.setHeaderVisible(true); // 表头显示
		table.setLinesVisible(true); // 表格线显示

		TableLayout tLayout = new TableLayout();
		table.setLayout(tLayout);

		tLayout.addColumnData(new ColumnWeightData(20));
		PTableColumn col1 = new PTableColumn(table, SWT.NONE);
		col1.setText("名称");
		col1.setName("cid");

		tLayout.addColumnData(new ColumnWeightData(30));
		PTableColumn col2 = new PTableColumn(table, SWT.NONE);
		col2.setText("标题");
		col2.setName("caption");
		
		tv1.addDragSupport(DND.DROP_MOVE,new Transfer[] { ObjectTransfer.getInstance() },new ViewerDragAdapter(tv1));
		tv1.addDropSupport(DND.DROP_MOVE,new Transfer[] { ObjectTransfer.getInstance() }, new PanTableOrderDropAdapter(tv1));

		table.reloadColumns();
		return tv1;
	}

	/**
	 * @param gridComposite
	 * @param cs
	 *            创建表格信息面板
	 */
	public void createDateGridComposite(BasePage basePage,BaseDataBind tabbind, Composite tabcp) {
		Color bgcolor = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		Group group1 = new Group(tabcp, SWT.NULL);
		group1.setText("表格頁面配置");
		group1.setLayout(new GridLayout(8, false));
		GridData gd = new GridData(GridData.FILL_BOTH);
		group1.setLayoutData(gd);
		
		Label lb=  new Label(group1,SWT.RIGHT);
		lb.setText("主键名:");
		lb.setBackground(bgcolor);
		Text pkName = new Text(group1, SWT.BORDER);
		pkName.setLayoutData(new GridData(80, 15));
		pkName.setText("sn");
		this.getDataBind().bind("pkName", pkName);
		
		lb=  new Label(group1,SWT.RIGHT);
		lb.setText("主键別名:");
		lb.setBackground(bgcolor);
		Text pkAliasName = new Text(group1, SWT.BORDER);
		pkAliasName.setText("sn");
		pkAliasName.setLayoutData(new GridData(80, 15));
		this.getDataBind().bind("pkAliasName", pkAliasName);
		
		lb=  new Label(group1,SWT.RIGHT);
		lb.setText("实体:");
		Text datatable = new Text(group1, SWT.BORDER);
		datatable.setLayoutData(new GridData(100, 15));
		tabbind.bind("tablename", datatable);
		
		lb=  new Label(group1,SWT.RIGHT);
		lb.setText("实体名称:");
		Text dataname = new Text(group1, SWT.BORDER);
		dataname.setLayoutData(new GridData(100, 15));
		tabbind.bind("dataname", dataname);
		
		
		BRComp br = new BRComp(group1, 8);
		Button enabSave = new Button(br,SWT.CHECK);
		enabSave.setText("启用储存功能");
		enabSave.setBackground(bgcolor);
		enabSave.setData(true);
		this.getDataBind().bind("enabSave", enabSave);
		
		Button enabCancel = new Button(br,SWT.CHECK);
		enabCancel.setText("启用取消功能");
		enabCancel.setBackground(bgcolor);
		enabCancel.setData(true);
		this.getDataBind().bind("enabCancel", enabCancel);
		
		Button enabDelete = new Button(br,SWT.CHECK);
		enabDelete.setText("启用刪除功能:");
		enabDelete.setBackground(bgcolor);
		enabDelete.setData(true);
		this.getDataBind().bind("enabDelete", enabDelete);
		
		Button defautSearch = new Button(br,SWT.CHECK);
		defautSearch.setText("加载时默认带出结果");
		defautSearch.setBackground(bgcolor);
		defautSearch.setData(true);
		this.getDataBind().bind("defautSearch", defautSearch);
		
		TabFolder tabFolder = new TabFolder(group1, SWT.NONE);
		gd.horizontalSpan=8;
		tabFolder.setLayoutData(gd);
		tabFolder.setBackground(bgcolor);
		
		//搜索表单配置
		createCondition(tabFolder);
		
		//表格配置
		createGrid(tabFolder);
	}
	
	/**
	 * 条件配置
	 */
	public void createCondition(TabFolder tabFolder){
		Color bgcolor = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		TabItem conditionTab = new TabItem(tabFolder, SWT.NONE);
		conditionTab.setData("id", "condition");
		conditionTab.setText("搜索表单配置");
		
		Composite parent = new Composite(tabFolder, SWT.NONE );
		parent.setLayout(new GridLayout(1,false));
		
		
		
		Composite composite = new Composite(parent, SWT.NONE );
		composite.setLayout(new RowLayout());
		composite.setBackground(bgcolor);
		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Label lb=  new Label(composite,SWT.RIGHT);
		lb.setText("列数(colModel):");
		Text colModel = new Text(composite, SWT.BORDER);
		colModel.setLayoutData(new RowData(200, 15));
		this.getDataBind().bind("colModel", colModel);

		ViewForm vform = new ViewForm(parent, SWT.NONE);
		vform.setLayout(new GridLayout());
		GridData vformData = new GridData(GridData.FILL_BOTH);
		vformData.horizontalSpan = 2;
		vformData.heightHint = 150;
		vformData.minimumHeight=100;
		vform.setLayoutData(vformData);

		PTableViewer tv2 = createTableViewer2(vform);
		CheckboxTableViewer ctv2 = new CheckboxTableViewer(tv2.getTable());
		BaseDataEntity data1 = this.getDataInstance();
		data1.spov("GridCrudPage1"+"tv2", tv2);
		data1.spov("GridCrudPage1"+"ctv2", ctv2);

		tv2.setContentProvider(new PllTableContentProvider()); // 内容器
		tv2.setLabelProvider(new PllTableLabelProvider(tv2));// 标签器

		
		SelectedColumnsActionGroup actionGroup1 = new SelectedColumnsActionGroup(this,"GridCrudPage1","GridCrudPage_condition");
		
		actionGroup1.fillContextMenu(new MenuManager());
		ToolBar toolbar1 = new ToolBar(vform, SWT.FLAT | SWT.RIGHT);
	
		toolbar1.setBackground(bgcolor);
		ToolBarManager toolbarManager = new ToolBarManager(toolbar1);
		actionGroup1.fillActionToolbars(toolbarManager,false);

		vform.setContent(tv2.getControl());
		vform.setTopLeft(toolbar1);
		
		conditionTab.setControl(parent);
	}
	
	//表格配置
	public void createGrid(TabFolder tabFolder){
		Color bgcolor = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		
		TabItem conditionTab = new TabItem(tabFolder, SWT.NONE);
		conditionTab.setData("id", "grid");
		conditionTab.setText("表格配置");
		
		Composite parent = new Composite(tabFolder, SWT.NONE );
		parent.setLayoutData(new GridData(GridData.FILL_BOTH));
		parent.setLayout(new GridLayout(1,false));
		
		Composite composite = new Composite(parent, SWT.NONE );
		composite.setLayout(new RowLayout());
		composite.setBackground(bgcolor);
		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Label lb=  new Label(composite,SWT.RIGHT);
		lb.setText("ID:");
		lb.setBackground(bgcolor);
		Text id = new Text(composite, SWT.BORDER);
		id.setLayoutData(new RowData(60, 15));
		id.setText("grid1");
		this.getDataBind().bind("id", id);	
		

		lb=  new Label(composite,SWT.RIGHT);
		lb.setText("数据源:");
		lb.setBackground(bgcolor);
		Text dataSet = new Text(composite, SWT.BORDER);
		dataSet.setLayoutData(new RowData(60, 15));
		dataSet.setText("default");
		dataSet.setEnabled(false);
		this.getDataBind().bind("dataSet", dataSet);
		
		Button defaultDataSet = new Button(composite,SWT.CHECK);
		defaultDataSet.setText("默认数据源");
		defaultDataSet.setBackground(bgcolor);
		defaultDataSet.setData(true);
		defaultDataSet.setData("dataSet", dataSet);
		defaultDataSet.setSelection(true);
		defaultDataSet.addSelectionListener(new PSelectionListener(null){
			@Override
			public void widgetSelected(SelectionEvent event) {
				Button btn1 = (Button)event.getSource();
				Text dataSet = (Text)btn1.getData("dataSet");
				if(btn1.getSelection()){
					dataSet.setText("default");
					dataSet.setEditable(false);
					dataSet.setEnabled(false);
				}else{
					dataSet.setText("");
					dataSet.setEditable(true);
					dataSet.setEnabled(true);
				}
			}
		});
		this.getDataBind().bind("defaultDataSet", defaultDataSet);
		
		
		Button loopFisrt = new Button(composite,SWT.CHECK);
		loopFisrt.setText("空值循环一次");
		loopFisrt.setBackground(bgcolor);
		loopFisrt.setData(true);
		this.getDataBind().bind("loopFisrt", loopFisrt);
		
		Button view = new Button(composite, SWT.CHECK);
		view.setText("锁表头");
		view.setBackground(bgcolor);
		view.setData("fixedRow");
		this.getDataBind().bind("view", view);
		
		Button toolbar =  new Button(composite, SWT.CHECK);
		toolbar.setText("显示默认工具栏");
		toolbar.setBackground(bgcolor);
		toolbar.setData(true);
		this.getDataBind().bind("toolbar", toolbar);
		
		ViewForm vform = new ViewForm(parent, SWT.NONE);
		vform.setLayout(new GridLayout());
		GridData vformData = new GridData(GridData.FILL_BOTH);
		vformData.horizontalSpan = 2;
		vformData.heightHint = 150;
		vformData.minimumHeight=100;
		vform.setLayoutData(vformData);

		PTableViewer tv2 = createTableViewer3(vform);
		CheckboxTableViewer ctv2 = new CheckboxTableViewer(tv2.getTable());
		BaseDataEntity data1 = this.getDataInstance();
		data1.spov("GridCrudPage2"+"tv2", tv2);
		data1.spov("GridCrudPage2"+"ctv2", ctv2);

		tv2.setContentProvider(new PllTableContentProvider()); // 内容器
		tv2.setLabelProvider(new PllTableLabelProvider(tv2));// 标签器

		
		SelectedColumnsActionGroup actionGroup1 = new SelectedColumnsActionGroup(this,"GridCrudPage2","GridCrudPage_grid");
		actionGroup1.fillContextMenu(new MenuManager());
		ToolBar toolbar1 = new ToolBar(vform, SWT.FLAT|SWT.RIGHT);
	
		toolbar1.setBackground(bgcolor);
		ToolBarManager toolbarManager = new ToolBarManager(toolbar1);
		actionGroup1.fillActionToolbars(toolbarManager,true);
		vform.setContent(tv2.getControl());
		vform.setTopLeft(toolbar1);
		conditionTab.setControl(parent);
	}

	/**
	 * @param group1
	 * @return 查詢條件配置
	 */
	public PTableViewer createTableViewer2(ViewForm vform) {
		PTableViewer tv2 = new PTableViewer(new PTable(vform, SWT.CHECK
				| SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER
				| SWT.FULL_SELECTION));
		PTable table = tv2.getPTable();
//		TableCursor cursor = new TableCursor(table, SWT.NONE);
//		cursor.addMouseListener(new PMouseListener() {
//			@Override
//			public void mouseDoubleClick(MouseEvent e) {
//				Utils.print(e.getSource().getClass().getName());
//			}
//		});
		
		table.setLayout(new GridLayout());
		GridData tableGridData = new GridData(GridData.FILL_BOTH);
		table.setLayoutData(tableGridData);
		table.setHeaderVisible(true);//表头显示
		table.setLinesVisible(true);//表格线显示

		PTableColumn col1 = new PTableColumn(table, SWT.NONE);
		col1.setText("名称");
		col1.setName("name");
		col1.setWidth(80);

		PTableColumn col2 = new PTableColumn(table, SWT.NONE);
		col2.setText("标题");
		col2.setName("caption");
		col2.setWidth(80);

		PTableColumn col3 = new PTableColumn(table, SWT.NONE);
		col3.setText("宽度");
		col3.setName("width");
		col3.setWidth(40);
		
		PTableColumn col4 = new PTableColumn(table, SWT.NONE);
		col4.setText("类型");
		col4.setName("xtype_text");
		col4.setWidth(60);
		
		PTableColumn col5 = new PTableColumn(table, SWT.NONE);
		col5.setText("為空");
		col5.setName("required_text");
		col5.setWidth(70);
		
		PTableColumn col6 = new PTableColumn(table, SWT.NONE);
		col6.setText("验证");
		col6.setName("valid");
		col6.setWidth(60);
		
		PTableColumn col7 = new PTableColumn(table, SWT.NONE);
		col7.setText("明細");
		col7.setName("detail");
		col7.setWidth(80);
		
		PTableColumn col8 = new PTableColumn(table, SWT.NONE);
		col8.setText("操作符");
		col8.setName("op_text");
		col8.setWidth(80);
		
		PTableColumn col9 = new PTableColumn(table, SWT.NONE);
		col9.setText("表别名");
		col9.setName("tablepix");
		col9.setWidth(80);
		
		PTableColumn col10 = new PTableColumn(table, SWT.NONE);
		col10.setText("栏位名");
		col10.setName("columnalias");
		col10.setWidth(80);
		
		table.reloadColumns();
		tv2.setColumnProperties(new String[] { "name", "caption", "width","xtype_text","required_text","valid","detail","op_text","tablepix","columnalias"});
		CellEditor[] editors = new CellEditor[10];
		editors[0] = new TextCellEditor(table, SWT.NONE);
		editors[1] = new TextCellEditor(table, SWT.NONE);
		editors[2] = new TextCellEditor(table, SWT.NONE);
		
		Map<String ,String> editorMap = QMDao.getInstance().getEditorMap();
		PanStringComboBoxCellEditor combo1 = new PanStringComboBoxCellEditor(table,editorMap.keySet().toArray(new String[]{}), SWT.READ_ONLY);
		Iterator it = editorMap.keySet().iterator();
		while(it.hasNext()){
			String key = (String)it.next();
			combo1.setData(key, editorMap.get(key));
		}
		editors[3]=combo1;
		
		PanStringComboBoxCellEditor combo2 = new PanStringComboBoxCellEditor(table,new String[] { "Y","N" }, SWT.READ_ONLY);
		combo2.setData("Y", "false");
		combo2.setData("N", "true");
		editors[4]=combo2;
		
		PanStringComboBoxCellEditor combo3 = new PanStringComboBoxCellEditor(table,new String[] { "--","等于","大于","大于等于","小于","小于等于","包含","以什么开头","以什么结尾"}, SWT.READ_ONLY);
		combo3.setData("--", "");
		combo3.setData("等于", "=");
		combo3.setData("大于", ">");
		combo3.setData("大于等于", ">=");
		combo3.setData("小于", "<");
		combo3.setData("小于等于", "<=");
		combo3.setData("包含", "like");
		combo3.setData("以什么开头", "starwith");
		combo3.setData("以什么结尾", "endwith");
		editors[7]=combo3;
		
		editors[8] = new TextCellEditor(table, SWT.NONE);
		editors[9] = new TextCellEditor(table, SWT.NONE);
		
		List list = new ArrayList();
		list.add("name");
		list.add("caption");
		list.add("width");
		list.add("xtype_text");
		list.add("required_text");
		list.add("op_text");
		list.add("tablepix");
		list.add("columnalias");
		
		tv2.setCellEditors(editors);
		tv2.setCellModifier(new SelectColumnsTableCellModifier(tv2,list));

		tv2.addDragSupport(DND.DROP_MOVE,new Transfer[] { ObjectTransfer.getInstance() },new ViewerDragAdapter(tv2));
		tv2.addDropSupport(DND.DROP_MOVE,new Transfer[] { ObjectTransfer.getInstance() }, new PanTableOrderDropAdapter(tv2));
		
		BaseDataEntity data = new BaseDataEntity();
		data.spov("tv2", tv2);
		tv2.getTable().addMouseListener(new PMouseListener(data){
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				PTableViewer tv2 = (PTableViewer)this.getData().gpov("tv2");
				PTable table = tv2.getPTable();
				int rowIndex = table.getSelectionIndex();
				int columnIndex = -1;
				int left = 0;
				for (int i = 0; i < table.getColumns().length; i++) {
					left += table.getColumns()[i].getWidth();
					if (e.x < left) {
						columnIndex = i;
						break;
					}
				}
				if (columnIndex == 6) {
					TableItem tabitem = table.getItem(rowIndex);
					BaseDataEntity data = (BaseDataEntity)tabitem.getData();
					if(data==null)return;
					
					BaseDialog dialog = getDialog(data);
					if (dialog != null) {
						//初始化控件 的值
						BaseDataEntity defaultData = dialog.getDefaultData();
						if(defaultData == null){
							defaultData = new BaseDataEntity();
							dialog.setDefaultData(defaultData);
						}
						
						data.CopyTo(defaultData, false);
						if(dialog.open() == BaseDialog.OK){
							table.ReloadTableItemRecord(tabitem, data);
						}
		            }
				}
			}
		});
		return tv2;
	}
	
	
	/**
	 * @param group1
	 * @return 表格配置
	 */
	public PTableViewer createTableViewer3(ViewForm vform) {
		PTableViewer tv2 = new PTableViewer(new PTable(vform, SWT.CHECK
				| SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER
				| SWT.FULL_SELECTION));
		PTable table = tv2.getPTable();
		
		table.setLayout(new GridLayout());
		GridData tableGridData = new GridData(GridData.FILL_BOTH);
		table.setLayoutData(tableGridData);
		table.setHeaderVisible(true);//表头显示
		table.setLinesVisible(true);//表格线显示

		PTableColumn col1 = new PTableColumn(table, SWT.NONE);
		col1.setText("名称");
		col1.setName("name");
		col1.setWidth(80);

		PTableColumn col2 = new PTableColumn(table, SWT.NONE);
		col2.setText("标题");
		col2.setName("caption");
		col2.setWidth(80);

		PTableColumn col3 = new PTableColumn(table, SWT.NONE);
		col3.setText("宽度");
		col3.setName("width");
		col3.setWidth(40);
		
		PTableColumn col4 = new PTableColumn(table, SWT.NONE);
		col4.setText("控件类型");
		col4.setName("xtype_text");
		col4.setWidth(70);
		
		PTableColumn col5 = new PTableColumn(table, SWT.NONE);
		col5.setText("編輯類型");
		col5.setName("editortype_text");
		col5.setWidth(70);
		
//		PTableColumn col6 = new PTableColumn(table, SWT.NONE);
//		col6.setText("验证");
//		col6.setName("valid");
//		col6.setWidth(60);
		
		PTableColumn col7 = new PTableColumn(table, SWT.NONE);
		col7.setText("明細");
		col7.setName("detail");
		col7.setWidth(80);
		
		table.reloadColumns();
		tv2.setColumnProperties(new String[] { "name", "caption", "width","xtype_text","editortype_text","detail"});
		CellEditor[] editors = new CellEditor[10];
		editors[0] = new TextCellEditor(table, SWT.NONE);
		editors[1] = new TextCellEditor(table, SWT.NONE);
		editors[2] = new TextCellEditor(table, SWT.NONE);
		
		Map<String ,String> editorMap = QMDao.getInstance().getEditorMap(true);
		PanStringComboBoxCellEditor combo1 = new PanStringComboBoxCellEditor(table,editorMap.keySet().toArray(new String[]{}), SWT.READ_ONLY);
		Iterator it = editorMap.keySet().iterator();
		while(it.hasNext()){
			String key = (String)it.next();
			combo1.setData(key, editorMap.get(key));
		}
		BaseDataEntity tempData = new BaseDataEntity();
		tempData.spov("combo1", combo1);
		tempData.spov("col5", col5);
		editors[3]=combo1;
		
		PanStringComboBoxCellEditor combo2 = new PanStringComboBoxCellEditor(table,new String[] {"--","静态","动态" }, SWT.READ_ONLY);
		combo2.setData("--", "");
		combo2.setData("静态", "static");
		combo2.setData("动态", "dynamic");
		editors[4]=combo2;
		
		List<String> list = new ArrayList<String>();
		list.add("name");
		list.add("caption");
		list.add("width");
		list.add("xtype_text");
		list.add("editortype_text");
		
		tv2.setCellEditors(editors);
		tv2.setCellModifier(new SelectColumnsTableCellModifier(tv2,list));
		
		tv2.addDragSupport(DND.DROP_MOVE,new Transfer[] { ObjectTransfer.getInstance() },new ViewerDragAdapter(tv2));
		tv2.addDropSupport(DND.DROP_MOVE,new Transfer[] { ObjectTransfer.getInstance() }, new PanTableOrderDropAdapter(tv2));
		
		BaseDataEntity data = new BaseDataEntity();
		data.spov("tv2", tv2);
		tv2.getTable().addMouseListener(new PMouseListener(data){
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				PTableViewer tv2 = (PTableViewer)this.getData().gpov("tv2");
				PTable table = tv2.getPTable();
				int rowIndex = table.getSelectionIndex();
				int columnIndex = -1;
				int left = 0;
				for (int i = 0; i < table.getColumns().length; i++) {
					left += table.getColumns()[i].getWidth();
					if (e.x < left) {
						columnIndex = i;
						break;
					}
				}
				if (columnIndex == 5) {
					TableItem tabitem = table.getItem(rowIndex);
					BaseDataEntity data = (BaseDataEntity)tabitem.getData();
					if(data==null)return;
					BaseDialog dialog = getDialog(data);
					if (dialog != null) {
						//初始化控件 的值
						BaseDataEntity defaultData = dialog.getDefaultData();
						if(defaultData == null){
							defaultData = new BaseDataEntity();
							dialog.setDefaultData(defaultData);
						}
						
						data.CopyTo(defaultData, false);
						if(dialog.open() == BaseDialog.OK){
							table.ReloadTableItemRecord(tabitem, data);
						}
		            }
				}
			}
		});
		return tv2;
	}
	
	public BaseDialog getDialog(BaseDataEntity row){
		String xtype = row.gpsv("xtype");
		String strClass = QMDao.getInstance().getXtypeDialogClassMap().get(xtype);
		BaseDialog dialog = null;
		if(!StringHelper.IsNullOrEmpty(strClass)){
			dialog = (BaseDialog)ObjectHelper.newInstance(strClass, new Object[]{Display.getDefault().getActiveShell(),row,"fillConfig"});
		}
		return dialog;
	}
	
	public FormItem getFormItem(String xtype){
		if(StringHelper.IsNullOrEmpty(xtype)) return null;
		String strClass =  QMDao.getInstance().getXtypeFormItemClassMap().get(xtype);
		FormItem formItem = null;
		if(!StringHelper.IsNullOrEmpty(strClass)){
			formItem = (FormItem)ObjectHelper.newInstance(strClass,null);
		}
		return formItem;
	}
	
	@Override
	public void reloadPageConfig() {
		super.reloadPageConfig();
		BasePageConfig pageConfig = this.getBaseWizard().getPageConfig();
		BaseDataEntity record = this.getDataBind().getRecord();
		QMDao dao = this.getQmDao();
		
		//是否要默认查询
		if(!pageConfig.gpbv("defautSearch", false)){
			pageConfig.spsv("searchoff", "//");
		}
		
		//查询语句
		String searchsql = pageConfig.gpsv("sql").trim();
		if(searchsql.indexOf("where") <0){
			searchsql += " where 1=1 ";
		}
		pageConfig.spsv("searchsql", searchsql);
		
		
		//1.生成查询表单 && 查询条件
		StringBuilderEx whr = new StringBuilderEx();
		CheckboxTableViewer condition_ctv2 =  (CheckboxTableViewer)this.getDataInstance().gpov("GridCrudPage1ctv2");//已选栏位表格选择器
		//form控件
		Tab tab = new Tab();
		tab.spsv("colModel", record.gpsv("colModel","*"));
		
		for (int j = 0; j < condition_ctv2.getTable().getItems().length; j++) {
			TableItem tabitem = condition_ctv2.getTable().getItems()[j];
			BaseDataEntity row = (BaseDataEntity)tabitem.getData();
			dao.registerPanICaption(row.gpsv("name"), row.gpsv("caption"));
			if(row != null){
				FormItem item = getFormItem(row.gpsv("xtype"));
				if(item != null){
					item.configItem(row,"default");
					if(row.gpsv("op").equals("like") || row.gpsv("op").equals("like") || row.gpsv("op").equals("like")){
						item.spsv("caption", item.GetExtValue("caption", "")+"(%)");
					}
					tab.addItem(item);
					whr.Append(this.getQmDao().getConditionSql(row)+"\r\n");
				}else{
					Utils.print(row.ToJSONString());
				}
			}
		}
		
		StringBuilderEx code = new StringBuilderEx();
		tab.onRender(code);
		pageConfig.OnSetProperty("sptabcode", code.toString());
		
		pageConfig.spsv("searchcondition", whr.toString());
		
		//2.生成表格
		FormItem table_layout = new FormItem();
		FormItem columnEditors = new FormItem();
		columnEditors.setNodeName("jm:columnEditors");
		
		Table table = new Table();//表
		table.configItem(record);
		
		CheckboxTableViewer grid_ctv2 =  (CheckboxTableViewer)this.getDataInstance().gpov("GridCrudPage2ctv2");//已选栏位表格选择器
		Thead thead = new Thead();
		Tbody tbody = new Tbody();
		
		TheadTr tr1 = new TheadTr();
		TbodyTr tr2 = new TbodyTr();
		//tr2.spsv("styleClass", "<%=\"0\".equals(ret.grsv(\"enab\")) ? \"bg0\" : \"\"%>");
		
		for (int j = 0; j < grid_ctv2.getTable().getItems().length; j++) {
			TableItem tabitem = grid_ctv2.getTable().getItems()[j];
			BaseDataEntity row = (BaseDataEntity)tabitem.getData();
			
			//表头
			TheadTd td1 = new TheadTd();
			td1.configItem(row);
			tr1.addItem(td1);
			
			//表身
			TbodyTd td2 = new TbodyTd();
			td2.configItem(row);
			tr2.addItem(td2);
			
			String xtype = row.gpsv("xtype");
			if(!StringHelper.IsNullOrEmpty(xtype)){
				FormItem item = getFormItem(row.gpsv("xtype"));
				
				if(item != null){
					if("static".equalsIgnoreCase(row.gpsv("editortype"))){
						item.configItem(row,"table-static");
						//靜態控件
						td2.addItem(item);
						td2.RemoveExtValue("cid");//如果有静态控件 要移除cid属性
					}else if("dynamic".equalsIgnoreCase(row.gpsv("editortype"))){
						item.configItem(row,"table-dynamic");
						FormItem columnEditor = new FormItem();
						columnEditor.setNodeName("jm:columnEditor");
						columnEditor.spsv("id", "e_"+row.gpsv("name"));
						columnEditor.spsv("type", xtype);
						columnEditor.addItem(item);
						columnEditors.addItem(columnEditor);
						
						//动态编辑控件配置
						StringBuilderEx strConfig = new StringBuilderEx();
						strConfig.Append("cid:'%1$s'",row.gpsv("name"));
						strConfig.Append(",editor:'%1$s'",xtype);
						strConfig.Append(",editorid:'e_%1$s'",row.gpsv("name"));
						td1.spsnnv("config", strConfig.toString());
					}
				}
			}
		}
		thead.addItem(tr1);
		tbody.addItem(tr2);
		table.addItem(thead);
		table.addItem(tbody);
		if(columnEditors.getItems() != null && columnEditors.getItems().size() >0){
			table_layout.addItem(columnEditors);
		}
		
		table_layout.addItem(table);
		code.Reset();
		table_layout.onRender(code);
		pageConfig.OnSetProperty("tablecode", code.toString());
	}

}
